package com.timeDeal.servlet;




import com.timeDeal.bean.UserBean;
import com.timeDeal.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * Created by Administrator on 2016/7/24.
 */

public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String zhanghao = request.getParameter("zhanghao");
        String mima = request.getParameter("mima");
        response.setContentType("text/html;charset=UTF-8");
        UserService us =new  UserService();
        try {
            boolean result = us.dologin(zhanghao,mima);
            if (result) {
                UserBean ub = new UserBean() ;
                ub.setYonghuming(us.getDetail(zhanghao,2)[0]);
                ub.setYonghuid(Integer.valueOf(us.getDetail(zhanghao,1)[0]));
                ub.setMima(mima);
                ub.setZhanghao(zhanghao);
                System.out.println(Integer.valueOf(us.getDetail(zhanghao,1)[0]));
                request.getSession().setAttribute("USER", ub);
                PrintWriter pw=response.getWriter();
                pw.print("<script language='javascript'>alert('登录成功');</script>");
                request.getRequestDispatcher("/user/zhujiemiangg.jsp?login=true").forward(request,response);
            } else {
                PrintWriter out = response.getWriter();
                out.print("<script language='javascript'>alert('用户名或密码错误');</script>");
                response.setHeader("refresh","0;url=/TimeDeal/user/login.jsp");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
